package com.ltmonitor.web.controller.command;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.BasicData;
import com.ltmonitor.entity.Department;
import com.ltmonitor.entity.JT809Command;
import com.ltmonitor.entity.VehicleData;
import com.ltmonitor.entity.WarnMsgUrgeTodoReq;
import com.ltmonitor.service.IJT809CommandService;
import com.ltmonitor.service.ITerminalService;
import com.ltmonitor.service.IVehicleDataService;
import com.ltmonitor.service.IWarnMsgUrgeTodoReqService;
import com.ltmonitor.util.DateUtil;
import com.ltmonitor.vo.PaginateResult;
import com.ltmonitor.web.util.JsonMessage;
import com.ltmonitor.web.util.OnlineUser;

/**
 * 报警督办【809运管平台接入】 1.显示上级平台下发的报警督办内容 2. 2.对上级平台的督办请求，点击确定进行应答
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/jt809AlarmTodo")
public class JT809AlarmTodoReqAction extends
		com.ltmonitor.web.controller.GenericAction {
	

	@Autowired
	private IWarnMsgUrgeTodoReqService warnMsgUrgeTodoReqService;

	/**
	 * jt809命令服务
	 */
	@Autowired
	private IJT809CommandService jt809CommandService;
	/**
	 * 查看报警督办应答
	 * 
	 * @return
	 */
	@RequestMapping("/view.action")
	public String view(long msgId,Model model) {
		WarnMsgUrgeTodoReq req =  this.warnMsgUrgeTodoReqService
				.load(msgId);

		BasicData bd = getBasicDataService().getBasicDataByCode(
				"" + req.getWarnSrc(), "GovAlarmSrc");
		String warnSrc = bd != null ? bd.getName() : "";
		Map entity = new HashMap();
		entity.put("warnSrc", warnSrc);
		entity.put("msgId", msgId);

		bd = getBasicDataService().getBasicDataByCode("" + req.getWarnType(),
				"GovAlarmType");
		String warnType = bd != null ? bd.getName() : "";
		entity.put("warnSrc", warnType);
		entity.put("warnTime", req.getWarnTime());
		entity.put("plateNo", req.getPlateNo());

		bd = getBasicDataService().getBasicDataByCode("" + req.getPlateColor(),
				"PlateColor");
		String plateColor = bd != null ? bd.getName() : "";
		entity.put("plateColor", plateColor);

		entity.put("supervicsionId", req.getSupervicsionId());
		entity.put("supervisionEndtime", req.getSupervisionEndtime());
		entity.put("supervisionLevel", req.getSupervisionLevel() == 0 ? "紧急"
				: "一般");
		entity.put("supervisor", req.getSupervisor());
		entity.put("supervisorTel", req.getSupervisorTel());
		entity.put("supervisorEmail", req.getSupervisorEmail());
		
		model.addAttribute("entity", entity);

		return "/command/jt809AlarmTodo";
	}

	// 报警督办应答

	@ResponseBody
	@RequestMapping("/send.action")
	public JsonMessage send(String plateNo, @RequestParam(defaultValue="0") long platformId,long msgId,int supervicsionId, int processState) {
		try {
			JT809Command jc = new JT809Command();
			jc.setCmd(0x1400);
			jc.setSubCmd(0x1401);
			StringBuilder sb = new StringBuilder();
			sb.append("" + supervicsionId + ";" + processState);
			jc.setPlateNo(plateNo);
			VehicleData vd = this.vehicleDataService
					.getVehicleByPlateNo(plateNo);
			if (vd != null)
				jc.setPlateColor((byte) vd.getPlateColor());
			jc.setCmdData(sb.toString());
			if (platformId == 0)
				platformId = this.getOnlineUser().getEntityId();
			jc.setUserId(platformId);
			OnlineUser onlineUser = getOnlineUser();
			if (onlineUser != null) {
				jc.setOwner(onlineUser.getName());
				// jc.setUserId(onlineUser.getEntityId());
			}
			jt809CommandService.save(jc);

			if (msgId > 0) {

				WarnMsgUrgeTodoReq req =  this.warnMsgUrgeTodoReqService
						.load(msgId);
				// req.setResult(WarnMsgUrgeTodoReq.STATUS_ACK);
				req.setResult(processState);
				this.warnMsgUrgeTodoReqService.saveOrUpdate(req);
			}
			return json(true, "");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return json(false, e.getMessage());
		}

	}

	/**
	 * 分页查询终端命令列表
	 * @param page 页码
	 * @param rows 每页行数
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/pagniate.action")
	public PaginateResult query( @RequestParam(value="page", defaultValue="1") int  page,
			@RequestParam(value="rows", defaultValue="10") int rows ) {
		Map params = new HashMap();
		String queryId = "warnMsgUrgTodoReq.selectMsgTodoReq";
		if (getOnlineUser() != null) {
			// 只查询用户自己下发的命令
			params.put("userId", getOnlineUser().getEntityId());
		}
		try {
			PaginateResult result = queryService.queryByPagination(queryId,
					params, page, rows);
			for (Object obj : result.getRows()) {

				Map rowData = (Map) obj;
				String warnSrc = "" + rowData.get("warnSrc");
				String warnType = "" + rowData.get("warnType");

				String supervisionLevel = "" + rowData.get("supervisionLevel");

				supervisionLevel = "0".equals(supervisionLevel) ? "紧急" : "一般";

				rowData.put("supervisionLevel", supervisionLevel);

				BasicData bd = getBasicDataService().getBasicDataByCode(
						warnSrc, "GovAlarmSrc");
				warnSrc = bd != null ? bd.getName() : "";
				rowData.put("warnSrc", warnSrc);

				if (rowData.get("depId") != null) {
					int depId = (Integer) rowData.get("depId");
					Department d = this.getDepartmentService().fetchDepartment(
							depId);
					if (d != null) {
						rowData.put("depName", d.getName());
					}
				}

				bd = getBasicDataService().getBasicDataByCode(warnType,
						"GovAlarmType");
				warnType = bd != null ? bd.getName() : "";
				rowData.put("warnType", warnType);

				int status = (Integer) rowData.get("result");
				String strStatus = "未应答";
				if (status == 1)
					strStatus = "已应答";
				rowData.put("result", strStatus);

				String plateColor = "" + rowData.get("plateColor");
				bd = getBasicDataService().getBasicDataByCode(plateColor,
						"plateColor");
				plateColor = bd != null ? bd.getName() : "";
				rowData.put("plateColor", plateColor);

				Date warnTime = (Date) rowData.get("warnTime");
				rowData.put("warnTime",
						DateUtil.toStringByFormat(warnTime, "MM-dd HH:mm:ss"));

				Date supervisionEndTime = (Date) rowData
						.get("supervisionEndtime");
				rowData.put("supervisionEndtime", DateUtil.toStringByFormat(
						supervisionEndTime, "MM-dd HH:mm:ss"));
			}
			return result;

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return new PaginateResult();
	}

	protected void SendCommand(long vehicleId, JT809Command tc) {
		if (vehicleId > 0) {
			VehicleData vd = vehicleDataService.load(vehicleId);
			tc.setPlateNo(vd.getPlateNo());
			tc.setPlateColor((byte) vd.getPlateColor());
		}
		OnlineUser onlineUser = getOnlineUser();
		if (onlineUser != null) {
			tc.setOwner(onlineUser.getName());
			tc.setUserId(onlineUser.getEntityId());
		}
		this.jt809CommandService.save(tc);
	}

}
